<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Page extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('page_m');
    }

    public function index($page = 1)
    {
        $get    = $this->input->get();
        $search = $where = [];
        if (isset($get['title']) && !empty($get['title'])) {
            $search['keyword'] = $keyword = $get['title'];
            $where             = "title like '%{$keyword}%'";
        }
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->page_m->total($where);
        $config['base_url']         = base_url('manage/Page/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $pages           = $this->page_m->select($where, $limit, $start);
        $data['search']   = $search;
        $data['pages'] = $pages;
        $this->load->view('page/index', $data);
    }

    public function add()
    {
        $this->load->view('page/form');
    }

    public function edit($page_id = null)
    {
        $page = $this->page_m->find($page_id);
        if (!$page) {
            show_message('参数错误！', site_url(X_URL . 'Page/index'));
        }
        $data['page']    = $page;
        $data['page_id'] = $page['id'];
        $this->load->view('page/form', $data);
    }

    public function del($page_id)
    {
        if ($page_id) {
            if ($this->page_m->delete_by(['id' => $page_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'Page/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'Page/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'Page/index'));
        }
    }

    public function update()
    {

        $post          = $this->input->post();
        $page_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $page_id    = isset($post['page_id']) ? intval($post['page_id']) : false;
        $page = [
            'title'      => $page_title,
            'img'        => $post['img'] ?? "",
            'content'    => $post['content'] ?? "",
        ];
        if (!$page_id) {
            // 新增
            $page['create_time'] = date("Y-m-d H:i:s", time());
            $pages               = $this->page_m->insert($page);
            if ($pages) {
                show_message('新增单页成功', site_url(X_URL . 'Page/index'), 1);
            } else {
                show_message('新增单页失败', site_url(X_URL . 'Page/add'));
            }
        } else {
            // 修改单页信息
            $affected_rows = $this->page_m->update(['id' => $page_id], $page);
            if ($affected_rows) {
                show_message('修改单页成功', site_url(X_URL . 'Page/index'), 1);
            } else {
                show_message('修改单页失败', site_url(X_URL . 'Page/edit'));
            }
        }
    }
}

